#!/bin/bash

#  Copyright (c) 2021- ~, XIWANG
#  
#  OLSR simulation batch all in one
#  Used in shell (NS2/setdest/cbrgen)
#  Clone from https://gitee.com/WangXi_Chn/olsr_-analysis.git
#  Note in https://www.yuque.com/wangxi_chn
#  
#  Change Logs:
#  Date             Author          Notes			Mail
#  2021-3-15        XiWang   	    forth version	WangXi_Chn@foxmail.com

# Param List
Version="V4.0"

# Init configure
mkdir -p cbr scene nam tr

# Function List
clearAllFun(){
    echo "clear: scene/cbr/nam/tr/result/png/zip"
    rm -rf scene/*
    rm -rf cbr/*
    rm -rf nam/*
    rm -rf tr/*
    rm -f *.txt
    rm -f *.png
    rm -f *.zip
}

clearResFun(){
    echo "clear: nam/tr/result/png/zip"
    rm -rf nam/*
    rm -rf tr/*
    rm -f *.txt
    rm -f *.png
    rm -f *.zip
}

clearSceFun(){
    echo "clear: scene/cbr"
    rm -rf scene/*
    rm -rf cbr/*
}

if [[ $1 == "help" ]]
then
    # Version & Help doc
    echo "---------------------------------------------------------------------------"
    echo "      Use Commands"
    echo "  NAME"
    echo "      OLSR Simulation batch all in one $Version"
    echo "      'sh batch_AllInOne.sh help' for more information"
    echo "  NOTE"
    echo "      Used in shell (NS2/Awk/Python3/sendEmail are needed)"
    echo "  DESCRIPTION"
    echo "      help    Version & Help doc"
    echo "      clean   Clear record folder"
    echo "      Clean   Clear machine cache"
    echo "      -v -n   Bulid Scene and run NS2 to simulate the effect of speed"
    echo "      -v      Use the existing Scene file to simulate the effect of speed"
    echo "      -s -n   Bulid Scene and run NS2 to simulate the effect of density"
    echo "      -s      Use the existing Scene file to simulate the effect of density"
    echo "      -t -n   Build Scene and run NS2 to simulate the throughput of net"
    echo "      -t      Use the existing Scene file to simulate the throughput of net"
    echo "      -vc FILENAME FILENAME   Compare two result producted by simulation about the effect of speed "
    echo "      -sc FILENAME FILENAME   Compare two result producted by simulation about the effect of density "
    echo "      -tc FILENAME FILENAME   Compare two simulation's result about throughput"
    echo "      -vcm NUM FILENAME(s)...	Compare multi-result producted by simulation about the effect of speed"
    echo "      -tcm NUM FILENAME(s)...	Compare multi-result producted by simulation about the throughput of net"
    echo "---------------------------------------------------------------------------"


elif [[ $1 == "clean" ]]
then
    clearAllFun

elif [[ $1 == "Clean" ]]
then
    echo "------ clear memory ------"
    free -m
    sync
    sudo bash -c "echo 1 > /proc/sys/vm/drop_caches"
    sudo bash -c "echo 2 > /proc/sys/vm/drop_caches"
    sudo bash -c "echo 3 > /proc/sys/vm/drop_caches"
    free -m
    
elif [[ $1 == "-v" ]]
then

    clearResFun

    # -----------------------------------------
    # Param List
    node_num=20
    times_num=50
    movement_range=500
    movement_MinSpeed=5
    movement_MaxSpeed=30
    speed_num=$(($movement_MaxSpeed / 5))
    # -----------------------------------------

    if [[ $2 == "-n" ]]
    then

        clearSceFun

        # Build Scene times
        for v in $(seq $movement_MinSpeed 5 $movement_MaxSpeed);
        do
            for i in $(seq 1 1 $times_num);
            do
                echo "----------------------------------------------------------------------------"
                echo "STEP: Building Scene N = ${node_num} V = ${v} S = ${movement_range} I = ${i}"
                setdest -n $node_num\
                		-p 3\
                		-M $v\
                        -t 300\
                        -x $movement_range\
                        -y $movement_range\
                        > scene/scene_olsr_n${node_num}_v${v}_s${movement_range}_i${i}
                ns cbrgen.tcl   -type cbr\
                                -nn $node_num\
                                -seed $seed\
                                -mc 15\
                                -rate 20.0\
                                > cbr/cbr_olsr_n${node_num}_v${v}_s${movement_range}_i${i}
            done
        done
    fi

    # Read Scene and Simulate Times
    for v in $(seq $movement_MinSpeed 5 $movement_MaxSpeed);
    do
        for i in $(seq 1 1 $times_num);
        do
        	rm -rf print_debug.txt
            echo "----------------------------------------------------------------------------"
            echo "STEP: Reading Scene N = ${node_num} V = ${v} S = ${movement_range} I = ${i}"
            echo "STEP: Simulating"
            ns olsr_simulate.tcl $node_num $v $movement_range $i

            # Catch exceptions
            if [[ $? == 0 ]]
            then
                sleep 25
                echo "STEP: Analying data"
                awk -f trace_analyze.awk tr/trace_olsr_n${node_num}_v${v}_s${movement_range}_i${i}.tr >> result_no_et_co.txt
            else
                echo "STEP: Simulate failed jump"
                echo "nan nan nan\n" >> result_no_et_co.txt
            fi
        done
    done

    echo "STEP: Draw the png to make data visualized"
    python3 draw_analysis.py -v $speed_num $times_num $node_num $movement_range $movement_MinSpeed $movement_MaxSpeed

    echo "STEP: Zip the record files"
    zip -r cbr.zip cbr
    zip -r scene.zip scene

    echo "STEP: Send email to record simulation result"
    sendEmail -xu 1098547591@qq.com\
                           -xp mhlarajafdccfhcf\
                           -t 1098547591@qq.com\
                           -u "OLSR simulation finished"\
                           -m "from Ubuntu 18.04 Destktop at $(date)"\
                           -s smtp.qq.com\
                           -f 1098547591@qq.com\
                           -a *.png *.zip result_no_et_co.txt

elif [[ $1 == "-s" ]]
then

    clearResFun

    # -----------------------------------------
    # Param List
    node_num=20
    times_num=1
    movement_Speed=30
    movement_MinRange=500
    movement_MaxRange=3000
    range_num=$(($movement_MaxRange / 500))
    # -----------------------------------------

    if [[ $2 == "-n" ]]
    then

        clearSceFun

        # Build Scene times
        for s in $(seq $movement_MinRange 500 $movement_MaxRange);
        do
            for i in $(seq 1 1 $times_num);
            do
                echo "----------------------------------------------------------------------------"
                echo "STEP: Building Scene N = ${node_num} V = ${movement_Speed} S = ${s} I = ${i}"
                setdest -n $node_num\
                        -p 3\
                        -M $movement_Speed\
                        -t 300\
                        -x $s\
                        -y $s\
                        > scene/scene_olsr_n${node_num}_v${movement_Speed}_s${s}_i${i}
                ns cbrgen.tcl   -type cbr\
                                -nn $node_num\
                                -seed $seed\
                                -mc 15\
                                -rate 20.0\
                                > cbr/cbr_olsr_n${node_num}_v${movement_Speed}_s${s}_i${i}
            done
        done
    fi

    # Read Scene and Simulate Times
    for s in $(seq $movement_MinRange 500 $movement_MaxRange);
    do
        for i in $(seq 1 1 $times_num);
        do
            echo "----------------------------------------------------------------------------"
            echo "STEP: Reading Scene N = ${node_num} V = ${movement_Speed} S = ${s} I = ${i}"
            echo "STEP: Simulation"
            ns olsr_simulate.tcl $node_num $movement_Speed $s $i

            # Catch exceptions
            if [[ $? = 0 ]]
            then
                sleep 25
                echo "STEP: Analying data"
                awk -f trace_analyze.awk tr/trace_olsr_n${node_num}_v${movement_Speed}_s${s}_i${i}.tr >> result_no_et_co.txt
            else
                echo "STEP: Simulate failed jump"
                echo "nan nan nan\n" >> result_no_et_co.txt
            fi
        done
    done

    echo "STEP: Draw the png to make data visualized"
    python3 draw_analysis.py -s $range_num $times_num $node_num $movement_Speed $movement_MinRange $movement_MaxRange

    echo "STEP: Zip the record files"
    zip -r cbr.zip cbr
    zip -r scene.zip scene

    echo "STEP: Send email to record simulation result"
    sendEmail -xu 1098547591@qq.com\
                           -xp mhlarajafdccfhcf\
                           -t 1098547591@qq.com\
                           -u "OLSR simulation finished"\
                           -m "from Ubuntu 18.04 Destktop at $(date)"\
                           -s smtp.qq.com\
                           -f 1098547591@qq.com\
                           -a *.png *.zip result_no_et_co.txt

elif [[ $1 == "-t" ]]
then

    clearResFun

    # -----------------------------------------
    # Param List
    node_num=20
    movement_Speed=20
    movement_Range=500
    i=1
    # -----------------------------------------

    if [[ $2 == "-n" ]]
    then

        clearSceFun

        # Build Scene times
        echo "STEP: Building Scene N = ${node_num} V = ${movement_Speed} S = ${movement_Range} I = ${i} ------"
        setdest -n $node_num\
                -p 3\
                -M $movement_Speed\
                -t 300\
                -x $movement_Range\
                -y $movement_Range\
                > scene/scene_olsr_n${node_num}_v${movement_Speed}_s${movement_Range}_i${i}
        ns cbrgen.tcl   -type cbr\
                        -nn $node_num\
                        -seed $seed\
                        -mc 15\
                        -rate 20.0\
                        > cbr/cbr_olsr_n${node_num}_v${movement_Speed}_s${movement_Range}_i${i}
    fi

    # Read Scene and Simulate Times
    echo "STEP: Reading Scene N = ${node_num} V = ${movement_Speed} S = ${movement_Range} I = ${i} ------"
    echo "STEP: Simulating"
    ns olsr_simulate.tcl $node_num $movement_Speed $movement_Range $i

    # Catch exceptions
    if [[ $? == 0 ]]
    then
        echo "STEP: Analying data"
        awk -f trace_analyze_throughput.awk\
            tr/trace_olsr_n${node_num}_v${movement_Speed}_s${movement_Range}_i${i}.tr >> result_throughput.txt
    else
        echo "STEP: Simulate failed jump"
        echo "nan nan nan" >> result_throughput.txt
    fi

    echo "STEP: Draw the png to make data visualized"
    python3 draw_analysis.py -t $node_num $movement_Speed $movement_Range

    echo "STEP: Zip the record files"
    zip -r cbr.zip cbr
    zip -r scene.zip scene
    zip -r tr.zip tr
    zip -r nam.zip nam
	
    echo "STEP: Send email to record simulation result"
    sendEmail -xu 1098547591@qq.com\
                           -xp mhlarajafdccfhcf\
                           -t 1098547591@qq.com\
                           -u "OLSR simulation finished"\
                           -m "from Ubuntu 18.04 Destktop at $(date)"\
                           -s smtp.qq.com\
                           -f 1098547591@qq.com\
                           -a *.png *.zip result_throughput.txt

elif [[ $1 == "-vc" ]]
then
    # -----------------------------------------
    # Param List
    node_num=20
    times_num=50
    movement_range=1000
    movement_MinSpeed=5
    movement_MaxSpeed=30
    speed_num=$(($movement_MaxSpeed / 5))
    # -----------------------------------------

    echo "STEP: Draw the png to make data visualized"
    python3 draw_analysis.py -c $2 $3 $times_num $speed_num $movement_MinSpeed $movement_MaxSpeed

    echo "STEP: Send email to record simulation result"
    sendEmail -xu 1098547591@qq.com\
                           -xp mhlarajafdccfhcf\
                           -t 1098547591@qq.com\
                           -u "OLSR simulation finished"\
                           -m "from Ubuntu 18.04 Destktop at $(date)"\
                           -s smtp.qq.com\
                           -f 1098547591@qq.com\
                           -a *.png

elif [[ $1 == "-sc" ]]
then
    # -----------------------------------------
    # Param List
    node_num=5
    times_num=5
    movement_Speed=30
    movement_MinRange=500
    movement_MaxRange=3000
    range_num=$(($movement_MaxRange / 500))
    # -----------------------------------------

    echo "STEP: Draw the png to make data visualized"
    python3 draw_analysis.py -c $2 $3 $times_num $range_num $movement_MinRange $movement_MaxRange

    echo "STEP: Send email to record simulation result"
    sendEmail -xu 1098547591@qq.com\
                           -xp mhlarajafdccfhcf\
                           -t 1098547591@qq.com\
                           -u "OLSR simulation finished"\
                           -m "from Ubuntu 18.04 Destktop at $(date)"\
                           -s smtp.qq.com\
                           -f 1098547591@qq.com\
                           -a *.png


elif [[ $1 == "-tc" ]]
then
    # -----------------------------------------
    # Param List
    node_num=20
    movement_Speed=20
    movement_Range=500
    i=1
    # -----------------------------------------

    echo "STEP: Draw the png to make data visualized"
    python3 draw_analysis.py -tc $2 $3 $node_num $movement_Speed $movement_Range

    echo "STEP: Send email to record simulation result"
    sendEmail -xu 1098547591@qq.com\
                           -xp mhlarajafdccfhcf\
                           -t 1098547591@qq.com\
                           -u "OLSR simulation finished"\
                           -m "from Ubuntu 18.04 Destktop at $(date)"\
                           -s smtp.qq.com\
                           -f 1098547591@qq.com\
                           -a *.png


elif [[ $1 == "-vcm" ]]
then
    # -----------------------------------------
    # Param List
    result_num=$2
    
    node_num=20
    times_num=50
    movement_range=500
    movement_MinSpeed=5
    movement_MaxSpeed=30
    speed_num=$(($movement_MaxSpeed / 5))
    
    echo "STEP: Draw the png to make data visualized"
    case $result_num in
    1)
        filename[0]=$3
        python3 draw_analysis.py -cm $2 $times_num $speed_num $movement_MinSpeed $movement_MaxSpeed\
            ${filename[0]}
        ;;
    2)
        filename[0]=$3
        filename[1]=$4
        python3 draw_analysis.py -cm $2 $times_num $speed_num $movement_MinSpeed $movement_MaxSpeed\
            ${filename[0]} ${filename[1]}
        ;;
    3)
        filename[0]=$3
        filename[1]=$4
        filename[2]=$5
        python3 draw_analysis.py -cm $2 $times_num $speed_num $movement_MinSpeed $movement_MaxSpeed\
            ${filename[0]} ${filename[1]} ${filename[2]}
        ;;
    4)
        filename[0]=$3
        filename[1]=$4
        filename[2]=$5
        filename[3]=$6
        python3 draw_analysis.py -cm $2 $times_num $speed_num $movement_MinSpeed $movement_MaxSpeed\
            ${filename[0]} ${filename[1]} ${filename[2]} ${filename[3]}
        ;;
    5)
        filename[0]=$3
        filename[1]=$4
        filename[2]=$5
        filename[3]=$6
        filename[4]=$7
        python3 draw_analysis.py -cm $2 $times_num $speed_num $movement_MinSpeed $movement_MaxSpeed\
            ${filename[0]} ${filename[1]} ${filename[2]} ${filename[3]} ${filename[4]}
        ;;
    6)
        filename[0]=$3
        filename[1]=$4
        filename[2]=$5
        filename[3]=$6
        filename[4]=$7
        filename[5]=$8
        python3 draw_analysis.py -cm $2 $times_num $speed_num $movement_MinSpeed $movement_MaxSpeed\
            ${filename[0]} ${filename[1]} ${filename[2]} ${filename[3]} ${filename[4]} ${filename[5]}
        ;;
    7)
        filename[0]=$3
        filename[1]=$4
        filename[2]=$5
        filename[3]=$6
        filename[4]=$7
        filename[5]=$8
        filename[6]=$9
        python3 draw_analysis.py -cm $2 $times_num $speed_num $movement_MinSpeed $movement_MaxSpeed\
            ${filename[0]} ${filename[1]} ${filename[2]} ${filename[3]} ${filename[4]} ${filename[5]} ${filename[6]}
        ;;
    8)
        filename[0]=$3
        filename[1]=$4
        filename[2]=$5
        filename[3]=$6
        filename[4]=$7
        filename[5]=$8
        filename[6]=$9
        filename[7]=$10
        python3 draw_analysis.py -cm $2 $times_num $speed_num $movement_MinSpeed $movement_MaxSpeed\
            ${filename[0]} ${filename[1]} ${filename[2]} ${filename[3]} ${filename[4]} ${filename[5]} ${filename[6]} ${filename[7]}
        ;;
    esac

    echo "STEP: Send email to record simulation result"
    sendEmail -xu 1098547591@qq.com\
                           -xp mhlarajafdccfhcf\
                           -t 1098547591@qq.com\
                           -u "OLSR simulation finished"\
                           -m "from Ubuntu 18.04 Destktop at $(date)"\
                           -s smtp.qq.com\
                           -f 1098547591@qq.com\
                           -a *.png

elif [[ $1 == "-tcm" ]]
then
    # -----------------------------------------
    # Param List
    result_num=$2
    
    node_num=5
    movement_Speed=20
    movement_Range=500
    # -----------------------------------------
   
    echo "STEP: Draw the png to make data visualized"
    case $result_num in
    1)
        filename[0]=$3
        python3 draw_analysis.py -tcm $2 $node_num $movement_Speed $movement_Range\
            ${filename[0]}
        ;;
    2)
        filename[0]=$3
        filename[1]=$4
        python3 draw_analysis.py -tcm $2 $node_num $movement_Speed $movement_Range\
            ${filename[0]} ${filename[1]}
        ;;
    3)
        filename[0]=$3
        filename[1]=$4
        filename[2]=$5
        python3 draw_analysis.py -tcm $2 $node_num $movement_Speed $movement_Range\
            ${filename[0]} ${filename[1]} ${filename[2]}
        ;;
    4)
        filename[0]=$3
        filename[1]=$4
        filename[2]=$5
        filename[3]=$6
        python3 draw_analysis.py -tcm $2 $node_num $movement_Speed $movement_Range\
            ${filename[0]} ${filename[1]} ${filename[2]} ${filename[3]}
        ;;
    5)
        filename[0]=$3
        filename[1]=$4
        filename[2]=$5
        filename[3]=$6
        filename[4]=$7
        python3 draw_analysis.py -tcm $2 $node_num $movement_Speed $movement_Range\
            ${filename[0]} ${filename[1]} ${filename[2]} ${filename[3]} ${filename[4]}
        ;;
    6)
        filename[0]=$3
        filename[1]=$4
        filename[2]=$5
        filename[3]=$6
        filename[4]=$7
        filename[5]=$8
        python3 draw_analysis.py -tcm $2 $node_num $movement_Speed $movement_Range\
            ${filename[0]} ${filename[1]} ${filename[2]} ${filename[3]} ${filename[4]} ${filename[5]}
        ;;
    7)
        filename[0]=$3
        filename[1]=$4
        filename[2]=$5
        filename[3]=$6
        filename[4]=$7
        filename[5]=$8
        filename[6]=$9
        python3 draw_analysis.py -tcm $2 $node_num $movement_Speed $movement_Range\
            ${filename[0]} ${filename[1]} ${filename[2]} ${filename[3]} ${filename[4]} ${filename[5]} ${filename[6]}
        ;;
    8)
        filename[0]=$3
        filename[1]=$4
        filename[2]=$5
        filename[3]=$6
        filename[4]=$7
        filename[5]=$8
        filename[6]=$9
        filename[7]=$10
        python3 draw_analysis.py -tcm $2 $node_num $movement_Speed $movement_Range\
            ${filename[0]} ${filename[1]} ${filename[2]} ${filename[3]} ${filename[4]} ${filename[5]} ${filename[6]} ${filename[7]}
        ;;
    esac

    echo "STEP: Send email to record simulation result"
    sendEmail -xu 1098547591@qq.com\
                           -xp mhlarajafdccfhcf\
                           -t 1098547591@qq.com\
                           -u "OLSR simulation finished"\
                           -m "from Ubuntu 18.04 Destktop at $(date)"\
                           -s smtp.qq.com\
                           -f 1098547591@qq.com\
                           -a *.png

#--------------------------------------------------------------------------------------

elif [[ $1 == "" ]]
then
    echo "Error: Commond can not be Empty, 'sh batch_AllInOne.sh help' for more information"
else
    echo "Error: Commond not found, 'sh batch_AllInOne.sh help' for more information"
fi

exit 0

#/************************ (C) COPYRIGHT 2021 XIWANG**************END OF FILE****/
